package com.sg.zhui.visitor.widget;

import android.content.Context;
import android.graphics.Rect;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.sg.zhui.visitor.R;
import com.sg.zhui.visitor.bean.ActionItem;

import java.util.ArrayList;

import okhttp3.internal.Util;

/*
 *Created by 李  on 2018/6/25.
 */
public class PullDownPopup extends PopupWindow {
    private Context mContext;

    //列表弹窗的间隔
    protected final int LIST_PADDING = 10;

    //实例化一个矩形
    private Rect mRect = new Rect();

    //坐标的位置（x、y）
    private final int[] mLocation = new int[2];

    //屏幕的宽度和高度
    private int mScreenWidth, mScreenHeight;

    //判断是否需要添加或更新列表子类项
    private boolean mIsDirty;

    //位置不在中心
    private int popupGravity = Gravity.NO_GRAVITY;

    //弹窗子类项选中时的监听
    private OnItemOnClickListener mItemOnClickListener;

    //定义列表对象
    private ListView mListView;

    //定义弹窗子类项列表
    private ArrayList<ActionItem> mActionItems = new ArrayList<ActionItem>();

    private int pos = 0;

    public PullDownPopup(Context context) {
        //设置布局的参数
        this(context, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public PullDownPopup(Context context, int width, int height) {
        this.mContext = context;

        //设置可以获得焦点
        setFocusable(true);
        //设置弹窗内可点击
        setTouchable(true);
        //设置弹窗外可点击
        setOutsideTouchable(true);

        //获得屏幕的宽度和高度
        mScreenWidth = PullDownPopUtil.getScreenWidth(mContext);
        mScreenHeight = PullDownPopUtil.getScreenHeight(mContext);

        //设置弹窗的宽度和高度
        this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);

        //this.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.shape_white_three));
        View view = LayoutInflater.from(mContext).inflate(R.layout.popup_pull_down, null);
        //设置弹窗的布局界面
        this.setContentView(view);

        initUI();
    }

    /**
     * 初始化弹窗列表
     */
    private void initUI() {
        mListView = (ListView) getContentView().findViewById(R.id.lv_list);

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int index, long arg3) {
                //点击子类项后，弹窗消失
                dismiss();
                pos = index;
                if (mItemOnClickListener != null)
                    mItemOnClickListener.onItemClick(mActionItems.get(index), index);
            }
        });
    }


    public void show(View view) {
        view.getLocationOnScreen(mLocation);
        mRect.set(mLocation[0], mLocation[1], mLocation[0] + view.getWidth(), mLocation[1] + 165);
        if (mIsDirty) {
            populateActions();
        }
        showAsDropDown(view, -10, 20);
    }

    /**
     * 设置弹窗列表子项
     */
    private void populateActions() {
        mIsDirty = false;

        //设置列表的适配器
        mListView.setAdapter(new BaseAdapter() {

            class ViewHolder {

                ImageView image_image;
                TextView tv_number_range;

            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {

                ViewHolder holder = null;
                if (convertView == null) {
                    holder = new ViewHolder();
                    convertView = LayoutInflater.from(mContext).inflate(R.layout.item_pull_down, null);
                    holder.tv_number_range = (TextView) convertView.findViewById(R.id.tv_number_range);

                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }


                ActionItem item = mActionItems.get(position);

                //设置文本文字
                holder.tv_number_range.setText(item.mTitle);
//                if (pos == position) {
//                    holder.tv_number_range.setBackgroundColor(mContext.getResources().getColor(R.color.blue_800));
//                    holder.tv_number_range.setTextColor(mContext.getResources().getColor(R.color.white));
//                } else {
//                    holder.tv_number_range.setBackgroundColor(mContext.getResources().getColor(R.color.transparent));
//                    holder.tv_number_range.setTextColor(mContext.getResources().getColor(R.color.nwb_personal_setup_textcolor6));
//                }

                return convertView;
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public Object getItem(int position) {
                return mActionItems.get(position);
            }

            @Override
            public int getCount() {
                return mActionItems.size();
            }
        });
    }

    /**
     * 添加子类项
     */
    public void addAction(ActionItem action) {
        if (action != null) {
            mActionItems.add(action);
            mIsDirty = true;
        }
    }

    /**
     * 清除子类项
     */
    public void cleanAction() {
        if (!mActionItems.isEmpty()) {
            mActionItems.clear();
            mIsDirty = true;
        }
    }

    /**
     * 根据位置得到子类项
     */
    public ActionItem getAction(int position) {
        if (position < 0 || position > mActionItems.size())
            return null;
        return mActionItems.get(position);
    }

    /**
     * 设置监听事件
     */
    public void setItemOnClickListener(OnItemOnClickListener onItemOnClickListener) {
        this.mItemOnClickListener = onItemOnClickListener;
    }

    /**
     * @author yangyu
     *         功能描述：弹窗子类项按钮监听事件
     */
    public static interface OnItemOnClickListener {
        public void onItemClick(ActionItem item, int position);
    }

}
